﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZDevice.Event;
using ZZMES.Data;

namespace ZZMES.TCP
{
    public class TCPHostService
    {
        private readonly IEventAggregator eventAggregator;
        private readonly DataProviderService dataProviderService;
        private MyTCPServer myTCP;
        public TCPHostService(IEventAggregator eventAggregator, DataProviderService dataProviderService)
        {
            this.eventAggregator = eventAggregator;
            this.dataProviderService = dataProviderService;
            myTCP = new MyTCPServer();
            var config = dataProviderService.communicateConfigs.Find(x => x.Name =="主机");
            myTCP.BuildServer(config.IP,config.Port);
            myTCP.ReceivedEvent += MyTCP_ReceivedEvent;
        }
        public void Send(string key,string msg) 
        {
            myTCP.Send(key, msg);
        }

        private void MyTCP_ReceivedEvent(string arg1, string arg2)
        {

            TcpEventArgs args = new TcpEventArgs();
            args.client = arg1;
            args.msg = arg2;
            eventAggregator.GetEvent<TcpEvent>().PublishAsync(args);
        }
    }
}
